package com.ckd.flyingtrip.charge.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.activity.BaseActivity;
import com.ckd.flyingtrip.charge.adapter.BatteryTimeAdapter;
import com.ckd.flyingtrip.charge.adapter.BatteryTypeAdapter;
import com.ckd.flyingtrip.charge.adapter.BatteryVoltageAdapter;
import com.ckd.flyingtrip.charge.javabean.BatteryInformationBean;
import com.ckd.flyingtrip.constants.Constants;
import com.ckd.flyingtrip.utils.BcUtils;
import com.ckd.flyingtrip.utils.DataCheck;
import com.ckd.flyingtrip.utils.ItemDecoration;
import com.ckd.flyingtrip.widge.SingleVolleyRequestQueue;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 * 电池信息
 * */
public class BatteryInformationActivity extends BaseActivity {

    @BindView(R.id.activity_time_recyclerView)
    RecyclerView time;
    @BindView(R.id.activity_type_recyclerView)
    RecyclerView type;
    @BindView(R.id.activity_voltage_recyclerView)
    RecyclerView voltage;
    @BindView(R.id.activity_money_recyclerView)
    TextView money;
    @BindView(R.id.toolber_color_title)
    TextView title;
    //缴费套餐
    private List<BatteryInformationBean.Data.ListMeal> dataBeans = new ArrayList<>();
//    private List<BatteryInformationBean.Data> dataTimeBeans = new ArrayList<>();
    private List<String> typeList = new ArrayList<>();
    private List<String> voltageList = new ArrayList<>();

    private BatteryTimeAdapter timeAdapter;
    private BatteryTypeAdapter batteryTypeAdapter;
    private BatteryVoltageAdapter batteryVoltageAdapter;

    private int positionType = 0;   //类型位置
    private int positionVoltage = 0;  //电压位置
    private int positionTime = 0;  //时间金额位置

    private String typeName;
    private String volageName;
    private String timeName;
    private String moneyName;
    private String costId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_battery_information);
        ButterKnife.bind(this);

        initView();
        initListener();
        initData();

    }

    private void initData() {
        StringRequest request = new StringRequest(Request.Method.POST, Constants.getmeallist_xcx, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.i("缴费套餐", s);
                try {
                    JSONObject json = new JSONObject(s);
                    String code = json.getString("code");
                    String msg = json.getString("msg");
                    if (code.equals("1")) {
                        BatteryInformationBean bean = new Gson().fromJson(s, BatteryInformationBean.class);

                        //缴费套餐
//                        dataTimeBeans = dataBeans.subList(0, dataBeans.size() - 1);
                        dataBeans = bean.getData().getListMeal();

                        if (timeAdapter != null)
                            timeAdapter.setList(dataBeans);

                        if (0 == positionTime){
                            moneyName = dataBeans.get(positionTime).getCostMoney() + "";
                            timeName = dataBeans.get(positionTime).getCostUseHour();
                            costId = dataBeans.get(positionTime).getCostId();
                        }

                        //初始化金额
                        money.setText( "¥" + dataBeans.get(0).getCostMoney());

                        //机柜信息
//                        BatteryInformationBean.Data data = dataBeans.get(dataBeans.size() - 1);
                        BatteryInformationBean.Data.ListCabinet data = bean.getData().getListCabinet();


                        //电池类型
                        String cabinetBatteryType = data.getCabinetBatteryType();
                        String[] typeArray = cabinetBatteryType.split(",");
                        for(int i=0,len=typeArray.length;i<len;i++){
                            typeList.add(typeArray[i]);
                        }
                        if (batteryTypeAdapter != null)
                            batteryTypeAdapter.setList(typeList);

                        if (0==positionType){
                            typeName = typeList.get(positionType);
                        }


                        //电池电压
                        String cabinetBatteryVoltage = data.getCabinetBatteryVoltage();
                        String[] voltageArray = cabinetBatteryVoltage.split(",");
                        for (int i = 0; i < voltageArray.length; i++) {
                            voltageList.add(voltageArray[i]);

                        }
                        if (batteryVoltageAdapter != null)
                            batteryVoltageAdapter.setList(voltageList);

                        if (0 == positionVoltage) {
                            volageName = voltageList.get(positionVoltage);
                        }
                        if (dataBeans.size() == 0)
                            toast("暂无数据！");
                    } else
                        toast(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("缴费套餐", "" + volleyError);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject json_url = new JSONObject();
                try {
                    json_url.put("userId", BcUtils.getUid());
                    json_url.put("userArea", Constants.CITYID);
                    json_url.put("cabinet", "CDZD08KXHD190401298");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                map.put("inputParameter", DataCheck.signCheck(json_url.toString()));

                return map;
            }
        };
        SingleVolleyRequestQueue.getInstance(this).addToRequestQueue(request);
    }


    private void initView() {
        title.setText("电池信息");

        //充电时间
        time.setLayoutManager(new GridLayoutManager(this, 3));
        time.addItemDecoration(new ItemDecoration(0, 15, 15, 0));
        timeAdapter = new BatteryTimeAdapter(this, dataBeans);
        time.setAdapter(timeAdapter);


        //电池类型
        type.setLayoutManager(new GridLayoutManager(this, 2));
        type.addItemDecoration(new ItemDecoration(0, 15, 15, 0));
        batteryTypeAdapter = new BatteryTypeAdapter(this, typeList);
        type.setAdapter(batteryTypeAdapter);

        //电池电压
        voltage.setLayoutManager(new GridLayoutManager(this, 3));
        voltage.addItemDecoration(new ItemDecoration(0, 15, 15, 0));
        batteryVoltageAdapter = new BatteryVoltageAdapter(this, voltageList);
        voltage.setAdapter(batteryVoltageAdapter);
    }

    private void initListener() {

        timeAdapter.setOnItemClickListener(new BatteryTimeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

                if (null != timeAdapter) {
                    timeAdapter.setPosition(position);
                }
                positionTime = position;
                money.setText("¥" + dataBeans.get(position).getCostMoney());
                moneyName = dataBeans.get(positionTime).getCostMoney() + "";
                timeName = dataBeans.get(positionTime).getCostUseHour();
                costId = dataBeans.get(positionTime).getCostId();

            }
        });

        batteryTypeAdapter.setOnItemClickListener(new BatteryTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                positionType = position;
                typeName = typeList.get(positionType);
            }
        });

        batteryVoltageAdapter.setOnItemClickListener(new BatteryVoltageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                positionVoltage = position;
                volageName = voltageList.get(positionVoltage);
            }
        });

    }

    @OnClick({R.id.toolber_color_back, R.id.btn_sumit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolber_color_back:
                Intent intent1 = new Intent();
                setResult(3, intent1);
                finish();
                break;
            case R.id.btn_sumit:
                Intent intent = new Intent(BatteryInformationActivity.this, PaymentInformationActivity.class);
                intent.putExtra("timeName",timeName);
                intent.putExtra("moneyName",moneyName);
                intent.putExtra("typeName",typeName);
                intent.putExtra("volageName",volageName);
                intent.putExtra("costId",costId);
                startActivityForResult(intent,1000);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1000 && resultCode==1001){
            finish();
        }
    }
}
